package sz.szsmk.citizencard.ui.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.index.IndexBar;
import sz.szsmk.citizencard.index.TitleItemDecoration;
import sz.szsmk.citizencard.model.network.bean.SubwayBean;
import sz.szsmk.citizencard.model.network.bean.SubwayStartItemBean;
import sz.szsmk.citizencard.model.network.bean.response.SubwayLineBean;
import sz.szsmk.citizencard.ui.adapter.CityAdapter;
import sz.szsmk.citizencard.ui.adapter.HeaderRecyclerAndFooterWrapperAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.CommonUtils;
import sz.szsmk.citizencard.util.DividerItemDecoration;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.MesageEventBus;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/7 16:51
 * description:轨交选择起点 排序展示页面
 */
@Route("SubwaySiteSorpListActivity")
public class SubwaySiteSorpListActivity extends IBaseActivity  {

    public static SubwaySiteSorpListActivity instance;
    ImageView back;
    TextView settingTitle;
    TextView bjTv;
    private RecyclerView subwayItemRv;
    private CityAdapter mAdapter;
    private LinearLayoutManager mManager;
    private List<SubwayStartItemBean> mDatas  = new ArrayList<>() ;

    private TitleItemDecoration mDecoration;

    /**
     * 右侧边栏导航区域
     */
    private IndexBar mIndexBar;

    /**
     * 显示指示器DialogText
     */
    private TextView mTvSideBarHint;

    private SubwayLineBean subwayLineBean;
    private String type;

    List<SubwayLineBean.LineListBean.GBean> data=new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.subway_site_sopt_activity;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        instance = this;

        back = findViewById(R.id.back);
        settingTitle = findViewById(R.id.setting_title);
        bjTv = findViewById(R.id.bj_tv);


        back.setOnClickListener(v -> finish());

        settingTitle.setOnClickListener(v ->
                JumpUtils.jump(SubwaySiteSorpListActivity.this,"SubwaySiteDetailActivity",0));

        bjTv.setVisibility(View.VISIBLE);
        bjTv.setText("线路排序");
        bjTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        type = getIntent().getStringExtra("type");
        if (type.equals("end")){
            settingTitle.setText("选择终点");
        }else {
            settingTitle.setText("选择起点");
        }
        subwayItemRv = (RecyclerView) findViewById(R.id.subway_site_list);
        subwayItemRv.setLayoutManager(mManager = new LinearLayoutManager(this));

        //使用indexBar
        mTvSideBarHint = (TextView) findViewById(R.id.tvSideBarHint);//HintTextView
        mIndexBar = (IndexBar) findViewById(R.id.indexBar);//IndexBar

        String subwayjson = CommonUtils.getJson(this, "subway.json");
        subwayLineBean = new Gson().fromJson(subwayjson, SubwayLineBean.class);

        //获取所有地铁站
        for(int i=0;i<subwayLineBean.getLine_list().size();i++){
            data.addAll(subwayLineBean.getLine_list().get(i).getG());
        }

        Map<String,SubwayStartItemBean> map = new HashMap<>();
        for(SubwayLineBean.LineListBean.GBean busStation:data){
            SubwayStartItemBean subwayStartItemBean = new SubwayStartItemBean();
            subwayStartItemBean.setTitle(busStation.getA());
            map.put(busStation.getA(),subwayStartItemBean);
        }
        mDatas.addAll(map.values());

        subwayItemRv.setAdapter( mAdapter = new CityAdapter(this, mDatas));
        mDecoration = new TitleItemDecoration(this, mDatas);
        subwayItemRv.addItemDecoration(mDecoration);

        //如果add两个，那么按照先后顺序，依次渲染。
        subwayItemRv.addItemDecoration(new DividerItemDecoration(SubwaySiteSorpListActivity.this, DividerItemDecoration.VERTICAL_LIST));

        //设置数据
        mIndexBar.setmPressedShowTextView(mTvSideBarHint)//设置HintTextView
                .setNeedRealIndex(true)//设置需要真实的索引
                .setmLayoutManager(mManager)//设置RecyclerView的LayoutManager
                .setmSourceDatas(mDatas);//设置数据源
        mIndexBar.invalidate();


        mAdapter.moreOnClick((view, position) -> {
            SubwayStartItemBean subwayStartItemBean =  mDatas.get(position);
            MesageEventBus mesageEventBus = new MesageEventBus();
            if (type.equals("end")){
                mesageEventBus.setEnd(subwayStartItemBean.getTitle());
            }else {
                mesageEventBus.setStart(subwayStartItemBean.getTitle());
            }
            EventBus.getDefault().postSticky(mesageEventBus);
            finish();
        });
    }


    @Override
    protected void updateViews() {

    }


}
